package io.gitee.guoguoleo.utils;

import cn.hutool.core.util.NumberUtil;
import cn.hutool.extra.spring.SpringUtil;
import io.gitee.guoguoleo.annotation.JokerLogic;
import io.gitee.guoguoleo.enums.SqlLike;
import io.gitee.guoguoleo.model.FieldInfo;
import io.gitee.guoguoleo.model.WhereFieldInfo;
import io.gitee.guoguoleo.properties.JokerProperties;
import net.sf.jsqlparser.expression.Expression;
import net.sf.jsqlparser.expression.LongValue;
import net.sf.jsqlparser.expression.StringValue;


public class SqlUtil {


    public static Expression getNotDelValue(FieldInfo delFieldInfo){
        JokerProperties jokerProperties = SpringUtil.getBean(JokerProperties.class);
        Expression expression;
        if (jokerProperties.getLogicNotDeleteValue() != null){
            if (NumberUtil.isNumber(String.valueOf(jokerProperties.getLogicNotDeleteValue()))) {
                expression = new LongValue(String.valueOf(jokerProperties.getLogicNotDeleteValue()));
            } else {
                expression = new StringValue(jokerProperties.getLogicNotDeleteValue().toString());
            }
        }else {
            JokerLogic jokerLogic = delFieldInfo.getField().getAnnotation(JokerLogic.class);
            if (jokerLogic != null && jokerLogic.noDelVal() != null){
                if (NumberUtil.isNumber(jokerLogic.noDelVal())) {
                    expression = new LongValue(String.valueOf(jokerLogic.noDelVal()));
                } else {
                    expression = new StringValue(jokerLogic.noDelVal());
                }
            }else {
                expression = new StringValue(Boolean.FALSE.toString());
            }
        }

        return expression;

    }

    public static Expression getDelValue(FieldInfo delFieldInfo){
        JokerProperties jokerProperties = SpringUtil.getBean(JokerProperties.class);
        Expression expression;
        if (jokerProperties.getLogicDeleteValue() != null){
            if (NumberUtil.isNumber(String.valueOf(jokerProperties.getLogicDeleteValue()))) {
                expression = new LongValue(String.valueOf(jokerProperties.getLogicDeleteValue()));
            } else {
                expression = new StringValue(jokerProperties.getLogicDeleteValue().toString());
            }
        }else {
            JokerLogic jokerLogic = delFieldInfo.getField().getAnnotation(JokerLogic.class);
            if (jokerLogic != null && jokerLogic.delVal() != null){
                if (NumberUtil.isNumber(jokerLogic.delVal())) {
                    expression = new LongValue(String.valueOf(jokerLogic.delVal()));
                } else {
                    expression = new StringValue(jokerLogic.delVal());
                }
            }else {
                expression = new StringValue(Boolean.TRUE.toString());
            }
        }

        return expression;

    }


    private static final String PERCENT = "%";
    /**
     * 连接like 值
     *
     * @param str     关键字
     * @param sqlLike like类型
     * @return like完整字段
     */
    public static StringValue concatLike(Object str, SqlLike sqlLike) {
        String value = switch (sqlLike) {
            case LEFT -> PERCENT + str;
            case RIGHT -> str + PERCENT;
            default -> PERCENT + str + PERCENT;
        };
        return new StringValue(value);
    }

}
